package com.qifenqian.turbot.module.adapter.restaurant

import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.qifenqian.turbot.R
import com.qifenqian.turbot.module.entity.response.CommentEntity
import kotlinx.android.synthetic.main.item_customer_comment.view.*
import org.jetbrains.anko.toast

/**
 * @author H
 * @createTime 2018/12/20 17:27
 * @description
 * @see
 * @since
 */
class CustomerCommentAdapter(data: List<CommentEntity>?) : BaseQuickAdapter<CommentEntity, BaseViewHolder>(R.layout.item_customer_comment, data) {

    private var mCommentImageChildRvAdapter: CommentImageChildRvAdapter? = null

    override fun convert(helper: BaseViewHolder, item: CommentEntity) {
        val view = helper.itemView
        Glide.with(mContext).load(item.userIcon).into(view.iv_item_customer_comment_head)
        view.tv_item_customer_comment_name.text = item.userName
        view.tv_item_customer_comment_time.text = item.createTime
        view.rab_item_customer_comment_stars.rating = item.commentLevel.toFloat()
        view.tv_item_customer_comment_content.text = item.commentContent
        mCommentImageChildRvAdapter = CommentImageChildRvAdapter(mContext, item.imageList)
        view.rv_item_customer_comment_image.layoutManager = LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false)
        view.rv_item_customer_comment_image.adapter = mCommentImageChildRvAdapter
        if (item.reply == null) {
            view.btn_item_customer_comment_reply.visibility = View.VISIBLE
            view.ly_item_customer_comment_reply.visibility = View.VISIBLE
            view.ly_item_customer_comment_reply_result.visibility = View.GONE
        } else {
            view.btn_item_customer_comment_reply.visibility = View.GONE
            view.ly_item_customer_comment_reply.visibility = View.GONE
            view.ly_item_customer_comment_reply_result.visibility = View.VISIBLE
            view.tv_item_customer_comment_reply_complete.text = item.reply!!.replyContent
        }
        view.btn_item_customer_comment_reply.setOnClickListener {
            if (onItemChildClickListener != null) {
                view.btn_item_customer_comment_reply.visibility = View.GONE
                view.ly_item_customer_comment_reply.visibility = View.VISIBLE
                view.btn_item_customer_comment_reply.visibility = View.GONE
            }
        }
        view.btn_item_customer_comment_cancel.setOnClickListener {
            if (onItemChildClickListener != null) {
                view.btn_item_customer_comment_reply.visibility = View.VISIBLE
                view.ly_item_customer_comment_reply.visibility = View.GONE
            }
        }
        view.btn_item_customer_comment_ok.setOnClickListener {
            if (view.et_item_customer_comment_reply_content.text.toString().isBlank()) {
                mContext.toast("请输入回复内容!")
                return@setOnClickListener
            }
            if (onReplyClickListener != null) {
                onReplyClickListener!!.onClick(view, helper.layoutPosition, view.et_item_customer_comment_reply_content.text.toString())
            }
        }
    }

    private var onReplyClickListener: OnReplyClickListener? = null

    fun setOnChildClickListener(listener: OnReplyClickListener) {
        onReplyClickListener = listener
    }

    interface OnReplyClickListener {
        fun onClick(view: View, position: Int, replyContent: String)
    }
}
